﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace jlynet.core.Library
{
    public class INIHelper
    {
        public string inipath;

        /// <summary>
        /// C＃申明INI文件的写操作函数WritePrivateProfileString()
        /// </summary>
        /// <param name="section">要写入的段落名</param>
        /// <param name="key">要写入的键，如果该key存在则覆盖写入</param>
        /// <param name="val">key所对应的值</param>
        /// <param name="filePath">INI文件的完整路径和文件名</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        /// <summary>
        /// C＃申明INI文件的写操作函数WritePrivateProfileSection()
        /// </summary>
        /// <param name="segName"></param>
        /// <param name="sValue"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName);

        /// <summary>
        /// C＃申明INI文件的读操作函数GetPrivateProfileString()
        /// </summary>
        /// <param name="section">要读取的段落名</param>
        /// <param name="key">读取异常的情况下的缺省值</param>
        /// <param name="def">读取异常的情况下的缺省值</param>
        /// <param name="retVal">key所对应的值，如果该key不存在则返回空值</param>
        /// <param name="size">值允许的大小</param>
        /// <param name="filePath">INI文件的完整路径和文件名</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// C＃申明INI文件的读操作函数GetPrivateProfileSection()
        /// </summary>
        /// <param name="segName"></param>
        /// <param name="buffer"></param>
        /// <param name="nSize"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);

        /// <summary>
        /// C＃申明INI文件的读操作函数GetPrivateProfileSectionNamesA()
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="iLen"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);

        [DllImport("kernel32")]
        private static extern uint GetPrivateProfileString(
            string lpAppName, // points to section name
            string lpKeyName, // points to key name
            string lpDefault, // points to default string
            byte[] lpReturnedString, // points to destination buffer
            uint nSize, // size of destination buffer
            string lpFileName  // points to initialization filename

        );


        public INIHelper(string INIPath)
        {
            inipath = INIPath;
        }

        /// <summary>
        /// 写入key
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }

        /// <summary>
        /// 读取所有段落下的所有key
        /// </summary>
        /// <returns></returns>
        public List<string> IniReadValues()
        {
            List<string> result = new List<string>();
            byte[] buf = new byte[65536];
            uint len = GetPrivateProfileString(null, null, null, buf, (uint)buf.Length, this.inipath);
            int j = 0;
            for (int i = 0; i < len; i++) {
                if (buf[i] == 0){
                    result.Add(Encoding.Default.GetString(buf, j, i - j));
                    j = i + 1;
                }
            }
            return result;
        }

        /// <summary>
        /// 读取指定段落下的所有key
        /// </summary>
        /// <param name="Section"></param>
        /// <returns></returns>
        public List<string> IniReadSectionValues(string Section)
        {
            List<string> result = new List<string>();
            byte[] buf = new byte[65536];
            uint lenf = GetPrivateProfileString(Section, null, "", buf, (uint)buf.Length, this.inipath);
            int j = 0;
            for (int i = 0; i < lenf; i++)
            {
                if (buf[i] == 0)
                {
                    string key = Encoding.Default.GetString(buf, j, i - j);
                    result.Add(key);
                    j = i + 1;
                }
            }
            return result;
        }

        /// <summary>
        /// 读取指定段落下的所有key的值
        /// </summary>
        /// <param name="Section"></param>
        /// <returns></returns>
        public Dictionary<string, Dictionary<string, object>> IniReadSectionValues2(string Section)
        {
            Dictionary<string, Dictionary<string, object>> root = new Dictionary<string, Dictionary<string, object>>();
            Dictionary<string, object> node = new Dictionary<string, object>();
            byte[] buf = new byte[65536];
            uint lenf = GetPrivateProfileString(Section, null, "", buf, (uint)buf.Length, this.inipath);
            int j = 0;
            for (int i = 0; i < lenf; i++)
            {
                if (buf[i] == 0)
                {
                    string key = Encoding.Default.GetString(buf, j, i - j);
                    string value = IniReadValue(Section, key);
                    node.Add(key, value);
                    j = i + 1;
                }
            }
            root.Add(Section, node);
            return root;
        }

        /// <summary>
        /// 读取指定段落下的指定key的值
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(1024);
            int i = GetPrivateProfileString(Section, Key, "", temp, 1024, this.inipath);
            return temp.ToString();
        }

        /// <summary>
        /// 删除ini文件下某段落的某个key所有内容
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="key"></param>
        public void delSectionKey(string sectionName, string key)
        {
            IniWriteValue(sectionName, key, null);
        }

        /// <summary>
        /// 删除ini文件下某段落的所有内容
        /// </summary>
        /// <param name="sectionName"></param>
        public void delSection(string sectionName)
        {
            IniWriteValue(sectionName, null, null);
        }

        /// <summary>
        /// 删除ini文件下的所有内容
        /// </summary>
        public void del()
        {
            IniWriteValue(null,null,null);
        }
    }
}
